#!/usr/bin/env python
#coding=utf-8

# 开发一个日志系统， 既要把日志输出到控制台， 还要写入日志文件
import logging
import logging.handlers


format_dict = { 1 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
                2 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
                3 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
                4 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'),
                5 : logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') }



# 创建一个logger
logger = logging.getLogger('test')
logger.setLevel(logging.DEBUG)

# 创建一个handler，用于写入日志文件
#fh = logging.FileHandler(logname)#不跟分割
#fh = logging.handlers.TimedRotatingFileHandler(logname, when='M', interval=1, backupCount=40)#按日分割
fh = logging.handlers.RotatingFileHandler('D:/loglog.log', maxBytes=10*1024, backupCount=40)#大小分割

fh.setLevel(logging.DEBUG)

# 再创建一个handler，用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter = format_dict[int(1)]
#dict
fh.setFormatter(formatter)
ch.setFormatter(formatter)

# 给logger添加handler
logger.addHandler(fh)
logger.addHandler(ch)

def getlog(self):
    return self.logger
#logger = Logger(logname='log.txt', loglevel='1', logger="fox").getlog()
#logger.info('teest')